#pragma once

#include "Common.hpp"
#include "Log.hpp"
#include "InetAddr.hpp"
#include <iostream>
#include <memory>
namespace SocketMoudule
{
    class Socket;
    using SockPtr = std::shared_ptr<Socket>;
    using namespace LogMoudule;
    class Socket
    {
    public:
        virtual ~Socket() = default;
        virtual void SocketOrDie() = 0;
        virtual bool BindOrDie(int port) = 0;
        virtual void SetSocketOpt() = 0;
        virtual bool ListenOrDie() = 0;
        virtual SockPtr Accepter(InetAddr *client) = 0;
        virtual int Send(const std::string &in_string) = 0;
        virtual int Recv(std::string *on_string) = 0;
        virtual void Close() = 0;
        virtual int Fd() = 0;
        void BulidTcpSocketMethod(int port)
        {
            SocketOrDie();
            SetSocketOpt();
            BindOrDie(port);
            ListenOrDie();
        }
    };
    class TcpSocket : public Socket
    {
    public:
        TcpSocket() : _sockfd(gsocketfd)
        {
        }
        TcpSocket(int sockfd) : _sockfd(sockfd)
        {
        }
        virtual void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd == gsocketfd)
            {
                LOG(LogLevel::ERROR) << "socket error!";
                Die(1);
            }
            LOG(LogLevel::DEBUG) << "socket success! ,sockfd is : " << _sockfd;
        }
        virtual bool BindOrDie(int port)
        {
            if (_sockfd == gsocketfd)
            {
                LOG(LogLevel::ERROR) << "_sockfd number error!";
                return false;
            }
            InetAddr addr(port);
            int n = ::bind(_sockfd, addr.GetNetAddrPtr(), addr.GetNetAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "bind error!";
                return false;
            }
            LOG(LogLevel::DEBUG) << "bind success! ,sockfd is : " << _sockfd;

            return true;
        }
        virtual void SetSocketOpt()
        {
        }
        virtual bool ListenOrDie()
        {
            if (_sockfd == gsocketfd)
            {
                LOG(LogLevel::ERROR) << "_sockfd number error!";
                return false;
            }
            int n = ::listen(_sockfd, 8);
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "listen error!";
                return false;
            }
            LOG(LogLevel::DEBUG) << "listen success! ,sockfd is : " << _sockfd;

            return true;
        }
        virtual SockPtr Accepter(InetAddr *client)
        {
            if (!client)
                return nullptr;
            sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int newsockfd = ::accept(_sockfd, CONV(&peer), &len);
            if (newsockfd < 0)
            {
                LOG(LogLevel::ERROR) << "accept error!";
                return nullptr;
            }
            LOG(LogLevel::DEBUG) << "Accepter success! ,sockfd is : " << _sockfd;

            client->SetAddr(peer, len);
            return std::make_shared<TcpSocket>(newsockfd);
        }
        virtual int Send(const std::string &in_string)
        {
            int n = ::send(_sockfd, in_string.c_str(), in_string.size(), 0);
            return n;
        }
        virtual int Recv(std::string *on_string)
        {
            char buffer[1024 * 8];
            int n = ::recv(_sockfd, buffer, sizeof(buffer), 0);
            if (n > 0)
            {
                buffer[n] = 0;
                *on_string = buffer;
            }
            return n;
        }
        virtual void Close()
        {
            ::close(_sockfd);
        }
        virtual int Fd()
        {
            return _sockfd;
        }

    private:
        int _sockfd;
    };
}